

def main():
	menu()
	bank = Bank("中国工商银行")
	while True:
		choice = input("请输入操作的序号[0-5]: ")
		if choice == "1":
			bank.query()
		elif choice == "2":
			bank.transfer()
		elif choice == "3":
			bank.get_money()
		elif choice == "4":
			bank.save_money()
		elif choice == "5":
			bank.add_user()
		elif choice == "0":
			print("退出系统!")
			break
		else:
			print("无效选择!")

	pass


def menu():
	print("*" * 40)
	print(f"*{' ' * 13}中国工商银行{' ' * 13}*")
	print(f"*{' ' * 13}账户管理系统{' ' * 13}*")
	print(f"*{' ' * 20}V1.0{' ' * 14}*")
	print("*" * 40)

	print(f"* 1.查询账户{' ' * 27}*")
	print(f"* 2.转账{' ' * 31}*")
	print(f"* 3.取款{' ' * 31}*")
	print(f"* 4.存款{' ' * 31}*")
	print(f"* 5.添加用户{' ' * 27}*")
	print(f"* 0.退出{' ' * 31}*")
	print("*" * 40)

class Bank:
	def __init__(self, name):
		self.name = name
		self.max = 100
		self.users = [
			User("u0001", "张三", "123456", 1000, Address("中国", "北京市", "朝阳区", "1号")),
			User("u0002", "李四", "123456", 2000, Address("中国", "北京市", "海淀区", "2号")),
			User("u0003", "王五", "123456", 3000, Address("中国", "北京市", "丰台区", "3号"))
		]

	def query(self):
		account = input("请输入账号: ")
		if not self.check_user(account):
			print("该用户不存在")
			return 1
		password = input("请输入密码: ")
		if not self.check_user(account, password):
			print("密码错误")
			return 2
		user = self.get_user_by_account(account)
		if user:
			print(f"当前账号: {user.account}")
			print(f"姓名: {user.name}")
			print(f"密码: {user.password}")
			print(f"余额: {user.deposit}")
			print(f"地址: {user.address.country} {user.address.province} {user.address.street} {user.address.number}")
			print(f"开户行: {user.bank}")
			return 0
		return 3

	def transfer(self):
		account = input("请输入转出账号: ")
		if not self.check_user(account):
			print("代码: 1")
			return 1
		account2 = input("请输入转入账号: ")
		if not self.check_user(account2):
			print("代码: 1")
			return 1
		password = input("请输入密码: ")
		if not self.check_user(account, password):
			print("代码: 2")
			return 2
		money = float(input("请输入转账金额: "))
		user = self.get_user_by_account(account)
		user2 = self.get_user_by_account(account2)
		if user and user.deposit < money:
			print("代码: 3")	
			return 3
		user.deposit -= money
		user2.deposit += money
		return 0

	def get_money(self):
		account= input("请输入账号: ")
		if not self.check_user(account):
			print("代码: 1")
			return 1
		password = input("请输入密码: ")
		if not self.check_user(account, password):
			print("代码: 2")	
			return 2
		money = float(input("请输入取款金额: "))
		user = self.get_user_by_account(account)
		if user and user.deposit >= money:
			user.deposit -= money
			print("代码: 0")
			return 0
		print("代码: 3")
		return 3

	def save_money(self, account, money):
		if self.check_user(account):
			user = self.get_user_by_account(account)
			if user:
				user.deposit += money
				print("代码: 0")
				return 0
			else:
				print("代码: 2")
				return 2
		print("代码: 1")
		return 1

	def add_user(self):
		user = User(
			account=input("请输入账号: "),
			name=input("请输入姓名: "),
			password=input("请输入密码: "),
			deposit=0,
			address=Address(
				country=input("请输入国家: "),
				province=input("请输入省份: "),
				street=input("请输入街道: "),
				number=input("请输入门牌号: ")
			)
		)
		if len(self.users) >= self.max:
			print("代码: 3")
			return 3
		elif self.check_user(user.account):
			print("代码: 2")
			return 2
		else:
			self.users.append(user)
			print("代码: 1")
			return 1
	
	def check_user(self, account, password=None):
		if password:
			user = self.get_user_by_account(account)
			if user and user.password == password:
					return True
		else:
			for user in self.users:
				if user.account == account:
					return True
		return False

	def get_user_by_account(self, account):
		for user in self.users:
			if user.account == account:
				return user
		return None


class User:
	def __init__(self, account, name, password, deposit, address):
		self.account = account
		self.name = name
		self.password = password
		self.deposit = deposit
		self.address = address
		self.bank = "昌平支行"

class Address:
	def __init__(self, country, province, street, number):
		self.country = country
		self.province = province
		self.street = street
		self.number = number

main()